package enemies
{
	import org.flixel.*;
	import org.flixel.plugin.photonstorm.*;
	
	import utilities.*;
	
	/**
	 * ...
	 * @author Chris Gamble
	 */
	public class Brute extends Enemy
	{
		public static const WONDERING:uint = 0;
		public static const ANGRY:uint = 1;
		public static const CHARGING:uint = 2;
		
		private var _chargeDestination:FlxPoint;
		private var _antiEmitter:AntiEmitter;
		
		public function Brute(i:uint = 0, j:uint = 0)
		{
			super(i, j);
			
			loadGraphic(Assets.ImgEnemyBrute, true, false, 25, 25);
			
			addAnimation("UP", [0, 1, 2, 1], 6);
			addAnimation("DOWN", [3, 4, 5, 4], 6);
			addAnimation("LEFT", [6, 7, 8, 7], 6);
			addAnimation("RIGHT", [9, 10, 11, 10], 6);
			addAnimation("CHARGE_UP", [12, 13], 6);
			addAnimation("CHARGE_DOWN", [14, 15], 6);
			addAnimation("CHARGE_LEFT", [16, 17, 18, 19], 6);
			addAnimation("CHARGE_RIGHT", [20, 21, 22, 23], 6);
			addAnimation("DYING", [24, 25, 26, 27, 28, 28, 28, 28], 6, false);
			play("DOWN");
			
			addText("ANGRY", "ARGH!!!");
			addText("FALLING", "NO!!!");
			
			state = WONDERING;
			_chargeDestination = null;
			
			_speed = 100;
			
			_antiEmitter = new AntiEmitter(0, 0, 5, 25, 100);
			_antiEmitter.makeColoredParticles(2, 2, 0xFFFF0000);
			_antiEmitter.start(false, 0.5, 0.01);
			
			toggleLight();
		}
		
		override public function update():void
		{
			super.update();
			
			if (!alive)
			{
				return;
			}
			
			if (state == CHARGING)
			{
				_animateMovements = false;
				chargingAnimations();
			}
			else
			{
				_animateMovements = true;
			}
			
			if (state == WONDERING)
			{
				if (isIdle)
				{
					if (thereAreMoreTreasures)
					{
						goToRandomTreasure();
					}
					else
					{
						if (!atExit)
						{
							while (!goToRandomExit())
							{
								
							}
						}
						else
						{
							moveTowardsFacing();
						}
					}
				}
				
				if (canSeePlayer(30, false))
				{
					state = ANGRY;
					play("DOWN");
					Registry.misc.add(_antiEmitter);
					_antiEmitter.callAll("kill");
					_antiEmitter.at(this);
					_timer.reset(1500);
					flicker(1.5);
					say("ANGRY");
					_chargeDestination = Player.midpoint;
					stopFollowingPath(true);
					stopMoving();
				}
			}
			else if (state == ANGRY)
			{
				_light.scale.x += _light.scale.x < 5 ? 0.5 : 0;
				_light.scale.y += _light.scale.y < 5 ? 0.5 : 0;
				_antiEmitter.at(this);
				if (isIdle)
				{
					stopMoving();
				}
				
				if (_timer.hasExpired)
				{
					state = CHARGING;
					
					offsetSpeed(150);
					goToPoint(_chargeDestination);
				}
			}
			else if (state == CHARGING)
			{
				_antiEmitter.at(this);
				_light.scale.x += _light.scale.x > 1 ? -0.5 : 0;
				_light.scale.y += _light.scale.y > 1 ? -0.5 : 0;
				if (notMoving)
				{
					state = WONDERING;
					Registry.misc.remove(_antiEmitter);
					offsetSpeed(-150, false);
					_light.scale.make(1, 1);
				}
				stopAtEdge();
			}
		}
		
		override public function kill():void
		{
			alive = false;
			play("DYING");
			say("FALLING");
			FlxG.play(Assets.SndFalling);
			Registry.misc.remove(_antiEmitter);
		}
		
		override public function overlapWithPlayer(p:Player):void
		{
			p.kill();
			FlxG.play(Assets.SndPunch);
		}
		
		private function chargingAnimations():void
		{
			if (velocity.y < 0)
			{
				play("CHARGE_UP");
				facing = UP;
			}
			else if (velocity.y > 0)
			{
				play("CHARGE_DOWN");
				facing = DOWN;
			}
			else if (velocity.x < 0)
			{
				play("CHARGE_LEFT");
				facing = LEFT;
			}
			else if (velocity.x > 0)
			{
				play("CHARGE_RIGHT");
				facing = RIGHT;
			}
		}
	}
}